<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">厂区名称：</label>
                                    <div class="layui-input-inline">
                                        <input name="name" class="layui-input" type="text" placeholder="请输入厂区名称"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                            <div class="query-rt">

                            </div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="factoryInfoBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
<!--                        <div class="lay-btn-rt">-->
<!--                            <button id="factoryInfoBtnExport" class="layui-btn icon-btn color-green">导出</button>-->
<!--                        </div>-->
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="factoryInfoTable" lay-filter="factoryInfoTable"></table>
        </div>
    </div>
</div>
<style>
    .factoryInfoForm .layui-form-label{
        width: 90px;
    }
    .factoryInfoForm .layui-input-block{
        margin-left: 120px;
    }
</style>
<!-- 表格操作列 -->
<script type="text/html" id="factoryInfoTableBar">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="factoryInfoForm">
    <form lay-filter="factoryInfoForm" class="layui-form model-form factoryInfoForm">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">厂区名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入厂区名称" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">厂区编号</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入厂区编号" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属公司</label>
            <div class="layui-input-block">
                <select name="companyId" id="companyId"></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">责任人</label>
            <div class="layui-input-block">
                <select name="userId" id="userId"></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">人流GPS定位</label>
            <div class="layui-input-block flexCt">
                <input id="personGps" name="personGps" placeholder="请选择人流GPS定位" type="text" class="layui-input" lay-verify="required" required readonly/>
                <button id="personGpsButton" type="button" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>打开地图</button>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">货流GPS定位</label>
            <div class="layui-input-block flexCt">
                <input id="commodityGps" name="commodityGps" placeholder="请选择货流GPS定位" type="text" class="layui-input" lay-verify="required" required readonly/>
                <button id="commodityGpsButton" type="button" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>打开地图</button>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注描述</label>
            <div class="layui-input-block">
            <input name="remake" placeholder="请输入备注描述" type="text" class="layui-input" />
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">是否启用</label>
            <div class="layui-input-block">
                <input name="deleted" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="启用|停用"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input name="sortNumber" placeholder="请输入排序" type="text" class="layui-input" lay-verify=""/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="factoryInfoFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>
<!-- js部分 -->

<script>
    var personGpsValue,commodityGpsValue = "";
    var getValue = function (type,lat,lng) {
        if(type===1){
            personGpsValue = lat+";"+lng
            document.getElementById("personGps").value =personGpsValue
        }else if(type===2){
            commodityGpsValue = lat+";"+lng
            document.getElementById("commodityGps").value =commodityGpsValue
        }
    }

    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;

        // 渲染表格
        var insTb = table.render({
            elem: '#factoryInfoTable',
            url: config.base + 'enterprise/system/admin/factoryInfo/list',
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            cellMinWidth: 100,
            cols: [[
                {field: 'name', align: 'center', title: '厂区名称'}
                ,{field: 'code', align: 'center', title: '厂区编号'}
                ,{field: 'companyName', align: 'center', title: '所属公司'}
                ,{field: 'userName', align: 'center', title: '责任人', width: 150}
                ,{field: 'personGps', align: 'center', title: '人流GPS定位', width: 180}
                ,{field: 'commodityGps', align: 'center', title: '货流GPS定位', width: 180}
                ,{field: 'createTime', align: 'center', title: '创建时间', width: 180}
                ,{align: 'center', toolbar: '#factoryInfoTableBar', title: '操作', width: 200, fixed: 'right'}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if(menuButton.indexOf("搜索") == -1){
                    $('.layui-form.toolbar').remove();
                }
                if(menuButton.indexOf("修改") == -1){
                    $("[lay-event='edit']").remove();
                }
                if(menuButton.indexOf("删除") == -1){
                    $("[lay-event='del']").remove();
                }
            }
        });

        // 工具条点击事件
        table.on('tool(factoryInfoTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 重置搜索
        form.on('submit(resets)', data => {
            insTb.reload({where: admin.resetSearch(data.field), page: {curr: 1}}, 'data');
        });

        //监听排序
        table.on('sort(factoryInfoTable)', function(obj){
            table.reload('factoryInfoTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 添加按钮点击事件
        $('#factoryInfoBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '700px',
                offset: '65px',
                title: data ? '修改厂区' : '添加厂区',
                content: $('#factoryInfoForm').html(),
                success: function () {
                    $('#personGpsButton').click(function () {
                        admin.open({
                            type: 2,
                            title:'地图',
                            content:'components/system/personGPS.html',
                            area: ['1000px','670px'],
                            offset: '65px'
                        })

                    })
                    $('#commodityGpsButton').click(function () {
                        admin.open({
                            type: 2,
                            title:'地图',
                            content:'components/system/commodityGPS.html',
                            area: ['1000px','670px'],
                            offset: '65px'
                        })

                    })

                    admin.req('user/admin/companyInfo/list', {}, function (res) {
                        if (res.code == 200) {
                            if (res.data.length > 0){
                                $.each(res.data,function (index,item) {
                                    $("#companyId").append(new Option(item.name,item.id));
                                })
                                form.render('select');
                            }
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GET');

                    admin.req('user/admin/user/list', {limit:9999}, function (res) {
                        if (res.code == 200) {
                            if (res.data.length > 0){
                                $.each(res.data,function (index,item) {
                                    $("#userId").append(new Option(item.username,item.id));
                                })
                                form.render('select');
                            }
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GET');


                    form.val('factoryInfoForm', data);
                    if(data){
                        var select = 'dd[lay-value=' + data.companyId + ']';
                        $("select[name='companyId']").siblings("div.layui-form-select").find('dl').find(select).click();

                        var select2 = 'dd[lay-value=' + data.userId + ']';
                        $("select[name='userId']").siblings("div.layui-form-select").find('dl').find(select2).click();
                    }
                    // 表单提交事件
                    form.on('submit(factoryInfoFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req(data ? 'enterprise/system/admin/factoryInfo/update' : 'enterprise/system/admin/factoryInfo/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('factoryInfoTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }



        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/system/admin/factoryInfo/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#factoryInfoBtnExport').click(function () {
            var checkRows = table.checkStatus('factoryInfoTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>